Previously encountered a business needs, the need to rely on the order to perform an unlimited number of ajax request to the server.
We know that in general JS asynchronous callback ajax function execution order cannot be guaranteed.
I used the queues and recursive methods based on jQuery to initially implement callback functions that sequentially execute asynchronous ajax requests.
If wrong, please criticize and correct :)
<script type="text/javascript">
$(document).ready(function() {
// Executes multipleajax commands in order, using a recursion because of the indefinite number
function send (action, arg2) {
// Encapsulate multiplecommands into array objects in sequence and execute them recursively
// Use the deferredobject to control the callback function
$ .when (send_action (action [0],arg2))
.done ( function () {
//Before ajax callback function to determine the length of the queue
if (action.length> 1) {
//queue length greater than 1, the first pop-up, continue to recursively executethe queue
action.shift ();
send (action,arg2);
}
}). fail ( function () {
//Queue elements in the request after the failure of the logic
//
//retry sending
//send (action, arg2);
//
//Ignore the error before proceeding
// if(action.length> 1) {
//queue length greater than 1, the first pop-up, continue to recursively executethe queue
//action.shift ();
//send (action, arg2);
//}
});
}
// handle each command'sajax request and callback function
function send_action (command, arg2) {
var dtd = $ .Deferred ();
$ .post (
"url",
{
command: command,
arg2: arg2
}
) .done ( function (json) {
json = $ .parseJSON(json);
// Processinglogic for each callback function
//
//
//
// Logicalend
dtd.resolve ();
}). fail ( function () {
// ajax The logicof the request failed
dtd.reject ();
});
return dtd.promise (); //Returns the promise of a Deferred object, preventing external modification ofthe state
}
});
</script>
Post your comments / questions
Recent Article
- How to create custom 404 error page in Django?
- Requested setting INSTALLED_APPS, but settings are not configured. You must either define..
- ValueError:All arrays must be of the same length - Python
- Check hostname requires server hostname - SOLVED
- How to restrict access to the page Access only for logged user in Django
- Migration admin.0001_initial is applied before its dependency admin.0001_initial on database default
- Add or change a related_name argument to the definition for 'auth.User.groups' or 'DriverUser.groups'. -Django ERROR
- Addition of two numbers in django python
Related Article